package org.kinglone.back.classify.action;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kinglone.back.article.model.Article;
import org.kinglone.back.article.service.ArticleService;
import org.kinglone.back.classify.model.Classify;
import org.kinglone.back.classify.service.ClassifyService;
import org.kinglone.common.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/classify")
public class ClassifyAction {

	@Resource(name="articleService")
	private ArticleService articleService;

	@Resource(name="classifyService")
	private ClassifyService classifyService;

	/**
	 * 跳转到后台分类管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectClassify.jspx")
	public ModelAndView selectClassify(HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/classify/classify.jsp");
		return modelAndView;
	}
	
	/**
	 * 跳转到后台添加分类页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/showAddClassify.jspx")
	public ModelAndView showAddArticle(HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/classify/addClassify.jsp");
		return modelAndView;
	}
	
	/**
	 * 分页查询分类
	 * @param request
	 * @return
	 */
	@RequestMapping("/seltClassifyList.jspx")
	public JSONMessageView seltClassifyList(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"查询失败!",null);
		try {
			String pageNo = request.getParameter("pageNo");
			String pageSize = request.getParameter("pageSize");
			PageQuery pageQuery = new PageQuery();
			pageQuery.setStart((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
			pageQuery.setLength(Integer.parseInt(pageSize));
			PageResults<Classify> list =classifyService.findClassifyByPage(pageQuery);
			JSONArray totalJsonArray = new JSONArray();
			JSONObject totalJsonObject = new JSONObject();
			JSONArray jsonArray=new JSONArray();
			for(Classify a : list.getResults()) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("sysid", a.getSysid());
				jsonObject.put("classifyName",a.getClassifyName());
				jsonObject.put("createtime",a.getTimeStr());
				jsonArray.add(jsonObject);
			}
			totalJsonObject.put("pagetotal", list.getPageCount());//总页数
			totalJsonObject.put("info", jsonArray);
			totalJsonArray.add(totalJsonObject);
			json.setCode(0);
			json.setMessage("查询成功!");
			json.setContent(totalJsonArray);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("服务器异常,请稍后再试!");
			return json;
		}
	}


	/**
	 * 跳转到后台修改分类页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateClassify.jspx")
	public ModelAndView updateClassify(HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/classify/updateClassify.jsp");
		String classifyId=request.getParameter("classifyId");
		if(classifyId != null){
			Classify classify = classifyService.get(classifyId);
			request.setAttribute("classify",classify);
		}
		return modelAndView;
	}

	

	/**
	 * 添加分类
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveOrUpdateClassify.jspx")
	public JSONMessageView saveOrUpdateClassify(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1, "保存失败!", null);
		try {
			String classifyId = request.getParameter("classifyId");
			String classifyName = request.getParameter("classifyName");
			if(StringUtil.isNullOrEmpty(classifyName)){
				json.setCode(-1);
				json.setMessage("请输入分类名称");
				return json;
			}
			if(StringUtil.isNullOrEmpty(classifyId)){
				Classify classify = new Classify();
				classify.setCreateTime(new Date());
				classify.setClassifyName(classifyName);	
				classifyService.save(classify);
				json.setCode(0);
				json.setMessage("保存成功!");
				return json;
			}else{
				Classify classify = classifyService.get(classifyId);
				classify.setClassifyName(classifyName);
				classify.setCreateTime(new Date());
				classifyService.update(classify);
				json.setCode(0);
				json.setMessage("修改成功!");
				return json;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("服务器异常，请稍候再试!");
			return json;
		}
	}

	/**
	 * 修改文章
	 * @param request
	 * @return
	 */
	
	/**
	 * 删除分类
	 * @param request
	 * @return
	 */
	@RequestMapping("/delClassify.jspx")
	public JSONMessageView delClassify(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"删除失败!",null);
		try {
			String classifyId = request.getParameter("classifyId");
			classifyService.deleteById(classifyId);
			List<Article> list = articleService.getByClassifyId(classifyId);
			if(list != null){
				for (int i = 0; i < list.size(); i++) {
					Article article = list.get(i);
					article.setClassifyId("");
					articleService.update(article);
				}
			}
			json.setCode(0);
			json.setMessage("删除成功!");
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}
	}

	

}
